package com.ksyzt.ksite2.client.news;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.ksyzt.gwt.client.common.MessageComposite;
import com.ksyzt.gwt.client.event.MessageEvent;
import com.ksyzt.ksite2.client.common.ServerProxy;
import com.ksyzt.ksite2.shared.modle.NEWSObj;
import com.ksyzt.ksite2.shared.modle.VW_NEWSObj;

public class PictureInfoEditor extends MessageComposite {

	private static PictureInfoEditorUiBinder uiBinder = GWT
			.create(PictureInfoEditorUiBinder.class);

	interface PictureInfoEditorUiBinder extends
			UiBinder<Widget, PictureInfoEditor> {
	}

	public PictureInfoEditor() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void message(String msg) {
		message.setText(msg);
	}

	@UiField
	Label message;

	@UiField
	Button btnCancel;
	@UiField
	TextBox txtTitle;

	@UiField
	TextArea txtCONTENT;

	@UiField
	TextBox txtLink;

	private AsyncCallback<VW_NEWSObj> m_update_callback = new AsyncCallback<VW_NEWSObj>() {

		@Override
		public void onSuccess(VW_NEWSObj result) {
			MessageEvent ev = new MessageEvent(MessageEvent.OK, result);
			fireEvent(ev);
		}

		@Override
		public void onFailure(Throwable caught) {
			message(caught.getMessage());

		}
	};

	@UiHandler("btnOK")
	void onOK(ClickEvent e) {

		if (txtTitle.getValue() == null || txtTitle.getValue().equals("")) {
			message("输入标题");
			return;
		}
		NEWSObj n = new NEWSObj();
		n.setID(m_n.getID());
		n.setLINK(txtLink.getValue());
		n.setTITLE(txtTitle.getValue());
		n.setCONTENT(txtCONTENT.getValue());
		ServerProxy.SYS_MANAGER.submitNews(n, m_update_callback);
	}

	VW_NEWSObj m_n;

	public void init(VW_NEWSObj n) {

		m_n = n;
		txtTitle.setValue(n.getTITLE());
		txtLink.setValue(n.getLINK());
		txtCONTENT.setValue(n.getCONTENT());
		message("");
	}

	@UiField
	Button btnOK;

	@UiHandler("btnCancel")
	void onCancel(ClickEvent e) {
		MessageEvent ev = new MessageEvent(MessageEvent.CANCEL, 0);
		fireEvent(ev);
	}
}
